<?php


namespace app\admin\controller;


use app\common\model\MoneyDetailed;
use PHPExcel;

class MoneyTx extends Common
{
    public function index()
    {
        if (input('get.us_name')) {
            $us_id = model("User")->where('us_nickname|us_tel', input('get.us_name'))->value('id');
            if (!$us_id) {
                $us_id = 0;
            }
            $this->map[] = ['us_id', '=', $us_id];
        }
        if (input('get.start') && input('get.end') == "") {
            $this->map[] = ['add_time', '>=', input('get.start')];
        }
        if (input('get.start') == "" && input('get.end')) {
            $this->map[] = ['add_time', '<=', input('get.end')];
        }
        if (input('get.start') && input('get.end')) {
            $this->map[] = ['add_time', 'between', array(input('get.start'), input('get.end'))];
        }
        $this->map[] = ['type','=',3];
        $this->map[] = ['tx_people_role','=',1];
        $this->map[] = ['tx_status','=',0];
        $list = MoneyDetailed::index($this->map,$this->order);
//        halt($list);

        foreach ($list as $k => $v) {
                $list[$k]['bank'] = db('banks')->where('id', $v['tx_account_id'])->where('type',1)->select();
                $list[$k]['us_nickname'] = model('User')->where('id', $v['us_id'])->value('us_nickname');
                $list[$k]['us_tel'] = model('User')->where('id', $v['us_id'])->value('us_tel');
        }
//        halt($list);
        $num = MoneyDetailed::num($this->map);
        $this->assign(array('list' => $list, 'a' => $num));
        return $this->fetch();
    }

    public function yunIndex()
    {
        if (input('get.us_name')) {
            $us_id = model("User")->where('us_nickname|us_tel', input('get.us_name'))->value('id');
            if (!$us_id) {
                $us_id = 0;
            }
            $this->map[] = ['us_id', '=', $us_id];
        }
        if (input('get.start') && input('get.end') == "") {
            $this->map[] = ['add_time', '>=', input('get.start')];
        }
        if (input('get.start') == "" && input('get.end')) {
            $this->map[] = ['add_time', '<=', input('get.end')];
        }
        if (input('get.start') && input('get.end')) {
            $this->map[] = ['add_time', 'between', array(input('get.start'), input('get.end'))];
        }
        $this->map[] = ['type','=',3];
        $this->map[] = ['tx_people_role','=',2];
        $this->map[] = ['tx_status','=',0];
        $list = MoneyDetailed::index($this->map,$this->order);

        foreach ($list as $k => $v) {
            $list[$k]['bank'] = db('banks')->where('id', $v['tx_account_id'])->where('type',2)->select();
            $list[$k]['us_nickname'] = model('Admin')->where('id', $v['us_id'])->value('ad_name');
            $list[$k]['us_tel'] = model('Admin')->where('id', $v['us_id'])->value('ad_tel');
        }
//        halt($list);
        $num = MoneyDetailed::num($this->map);
        $this->assign(array('list' => $list, 'a' => $num));
        return $this->fetch();

    }


    // 会员通过提现
    public function change()
    {
        if (is_post()) {
            $data[input('post.key')] = input('post.value');
            $data['tx_adopt_time'] = date('Y-m-d H:i:s');
            $rel = model('MoneyDetailed')->save($data, ['id' => input('post.id')]);
            if ($rel) {
                return ['code' => '1', 'msg' => '审批成功'];
            }else{
                return ['code' => '0', 'msg' => '审批失败'];
            }
        }
    }

    // 会员驳回提现
    public function change2()
    {
        if (is_post()) {
            $tx_id = input('post.id');  //  提现id
            $us_id = model('MoneyDetailed')->where('id', $tx_id)->value('us_id');

            $data['y_pay_money'] = model('user')->where('id', $us_id)->value('us_all_get');
            $data[input('post.key')] = input('post.value');
            $data['tx_back_time'] = date('Y-m-d H:i:s');
            $info = model('MoneyDetailed')
                ->where('id', input('post.id'))
                ->find();
            $rst = model('User')->where('id', $info['us_id'])->setInc('us_all_get', $info['money']);
            $data['over_pay_money'] = model('User')->where('id', $us_id)->value('us_all_get');

            $up = model('MoneyDetailed')->save($data, ['id' => input('post.id')]);
            if ($rst && $up) {
                return ['code' => '1', 'msg' => '驳回成功'];
            } else {
                return ['code' => '0', 'msg' => '驳回失败'];
            }
        }
    }






    // 展示提现详情
    public function recording()
    {
        if (input('get.keywords')) {
            $us_id = model("User")->where('us_nickname|us_tel', input('get.keywords'))->value('id');
            if (!$us_id) {
                $us_id = 0;
            }
            $this->map[] = ['us_id', '=', $us_id];
        }
        if (is_numeric(input('get.tixian_type'))) {
            $this->map[] = ['tx_status', '=', input('get.tixian_type')];
        }
        if (input('get.start') && input('get.end') == "") {
            $this->map[] = ['add_time', '>=', input('get.start')];
        }
        if (input('get.start') == "" && input('get.end')) {
            $this->map[] = ['add_time', '=<', input('get.end')];
        }
        if (input('get.start') && input('get.end')) {
            $this->map[] = ['add_time', 'between', array(input('get.start'), input('get.end'))];
        }
//        if (input('get.wa_type') != "") {
//            $this->map[] = ['wa_type', '=', input('get.wa_type')];
//        }
        $this->map[] = ['type', '=', 3];
        $list = MoneyDetailed::index($this->map,$this->order);
//        halt($list);

        foreach ($list as $k => $v) {

            if ($v['tx_people_role'] == 1) {
//                $account = 'we_account';
                $list[$k]['bank'] = db('banks')->where('id', $v['tx_account_id'])->where('type',1)->select();
                $list[$k]['us_nickname'] = model('User')->where('id', $v['us_id'])->value('us_nickname');
                $list[$k]['us_tel'] = model('User')->where('id', $v['us_id'])->value('us_tel');
            } elseif ($v['tx_people_role'] == 2) {
                $list[$k]['bank'] = db('banks')->where('id', $v['tx_account_id'])->where('type',2)->select();
                $list[$k]['us_nickname'] = model('Admin')->where('id', $v['us_id'])->value('ad_name');
                $list[$k]['us_tel'] = model('Admin')->where('id', $v['us_id'])->value('ad_tel');
            }
        }
        $money = MoneyDetailed::num($this->map);
        $this->assign(['list' => $list, 'money'=>$money]);
        return $this->fetch();
    }

    public function excel(){//导出Excel

        $start = input('get.time_start');
        $end = input('get.time_send');
        $start_time = date("Y-m-d H:i:s",$start/1000);
        $end_time =$end!='NaN'? date("Y-m-d H:i:s",$end/1000):date('Y-m-d H:i:s');

        if (is_numeric(input('get.tixian_type'))) {
            $this->map[] = ['tx_status', '=', input('get.tixian_type')];
        }
        if (input('get.keywords')) {
            $us_id = model("User")->where('us_nickname|us_tel', input('get.keywords'))->value('id');
            if (!$us_id) {
                $us_id = 0;
            }
            $this->map[] = ['us_id', '=', $us_id];
        }

        if ($start && $start == "") {
            $this->map[] = ['add_time', '>=', $start_time];
        }
        if ($start == "" && $end) {
            $this->map[] = ['add_time', '<=', $end_time];
        }
        if ($start && $end) {
            $this->map[] = ['add_time', 'between', array($start_time, $end_time)];
        }

        $this->map[] = ['type', '=', 3];
        $list =  MoneyDetailed::where($this->map)
            ->order($this->order)
           ->select();



        foreach ($list as $k => $v) {

            if ($v['tx_people_role'] == 1) {

                //  ["card_holder"] => string(6) "王耀"
                //              ["bank_name"] => string(18) "中国农业银行"
                //              ["bank_account"] => string(19) "6228482371262681004"
                //              ["bank_branch_name"] => string(30) "中国农业银行郑州分行"

//                $account = 'we_account';
                $list[$k]['card_holder'] = db('banks')->where('id', $v['tx_account_id'])->where('type',1)->value('card_holder');
                $list[$k]['bank_name'] = db('banks')->where('id', $v['tx_account_id'])->where('type',1)->value('bank_name');
                $list[$k]['bank_account'] = db('banks')->where('id', $v['tx_account_id'])->where('type',1)->value('bank_account');
                $list[$k]['bank_branch_name'] = db('banks')->where('id', $v['tx_account_id'])->where('type',1)->value('bank_branch_name');

                $list[$k]['us_nickname'] = model('User')->where('id', $v['us_id'])->value('us_nickname');
                $list[$k]['us_tel'] = model('User')->where('id', $v['us_id'])->value('us_tel');
            } elseif ($v['tx_people_role'] == 2) {
                $list[$k]['card_holder'] = db('banks')->where('id', $v['tx_account_id'])->where('type',2)->value('card_holder');
                $list[$k]['bank_name'] = db('banks')->where('id', $v['tx_account_id'])->where('type',2)->value('bank_name');
                $list[$k]['bank_account'] = db('banks')->where('id', $v['tx_account_id'])->where('type',2)->value('bank_account');
                $list[$k]['bank_branch_name'] = db('banks')->where('id', $v['tx_account_id'])->where('type',2)->value('bank_branch_name');
                $list[$k]['us_nickname'] = model('Admin')->where('id', $v['us_id'])->value('ad_name');
                $list[$k]['us_tel'] = model('Admin')->where('id', $v['us_id'])->value('ad_tel');
            }
        }

        $xlsName  = "提现记录";
        $xlsCell  = array(
            array('us_nickname', '用户昵称'),
            array('us_tel', '用户账户'),
            array('tx_people_role', '提现人角色'),
            array('money', '提现金额'),
            array('tx_status', '提现审核状态'),
            array('card_holder', '持卡人姓名'),
            array('bank_name', '开户行'),
            array('bank_account', '银行卡号'),
            array('bank_branch_name', '银行分行'),
        );

        $xlsData  = array();
        // halt($info['list']);
//        unset($info['list']['total']);
        foreach ($list as $k => $v) {
            $xlsData[$k]['us_nickname'] = $v['us_nickname'];
            $xlsData[$k]['us_tel'] = $v['us_tel'];
            if ($v['tx_people_role'] == 1) {
                $xlsData[$k]['tx_people_role'] = '用户提现';
            }else{
                $xlsData[$k]['tx_people_role'] = '供应商提现';
            }

            $xlsData[$k]['money'] = $v['money'];
            if ($v['tx_status'] == 1) {
                $xlsData[$k]['tx_status'] = '已通过';
            }elseif($v['tx_status'] == 2){
                $xlsData[$k]['tx_status'] = '已驳回';
            }else{
                $xlsData[$k]['tx_status'] = '待审核';
            }
            $xlsData[$k]['card_holder'] = $v['card_holder'];

            $xlsData[$k]['bank_name'] = $v['bank_name'];
            $xlsData[$k]['bank_account'] = $v['bank_account'];
            $xlsData[$k]['bank_branch_name'] = $v['bank_branch_name'];
        }

        $this->exportExcel($xlsName,$xlsCell,$xlsData);

    }

    function exportExcel($expTitle,$expCellName,$expTableData){
        //include_once EXTEND_PATH.'PHPExcel/PHPExcel.php';//方法二
        $xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称
        $fileName = $expTitle.date('_YmdHis');//or $xlsTitle 文件名称可根据自己情况设定
        $cellNum = count($expCellName);
        $dataNum = count($expTableData);
        $num = $dataNum + 3;
        $objPHPExcel = new PHPExcel();//方法一
        //$objPHPExcel = new \PHPExcel();//方法二
        $cellName = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ');
        $objPHPExcel->getActiveSheet(0)->mergeCells('A1:'.$cellName[$cellNum-1].'1');//合并单元格
        $objPHPExcel->getActiveSheet(0)->mergeCells('A'.$num.':'.$cellName[$cellNum-1].$num);//合并单元格
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $expTitle.'  表格生成时间:'.date('Y-m-d H:i:s'));
        // halt($num);
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A'.$num, '制表人:                      ' . '会计：                      ' . '出纳：                      ' . '审核：                      ');
        for($i=0;$i<$cellNum;$i++){
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i].'2', $expCellName[$i][1]);
        }
        // Miscellaneous glyphs, UTF-8
        for($i=0;$i<$dataNum;$i++){
            for($j=0;$j<$cellNum;$j++){
                $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j].($i+3), $expTableData[$i][$expCellName[$j][0]]);
            }
        }
        ob_end_clean();//这一步非常关键，用来清除缓冲区防止导出的excel乱码
        header('pragma:public');
        header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$xlsTitle.'.xls"');
        header("Content-Disposition:attachment;filename=$fileName.xls");//"xls"参考下一条备注
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');//"Excel2007"生成2007版本的xlsx，"Excel5"生成2003版本的xls
        $objWriter->save('php://output');
        exit;
    }


}